/*
 * Name        : TaskEventHandler.java
 * Classes     : TaskEventHandler
 * Version     : 1.0
 * Date        : 2013-08-29
 *
 * Copyright 2013 CMCC.  All rights reserved.
 * This material, including documentation and any related computer
 * programs, is protected by copyright controlled by CMCC.  All
 * rights are reserved.  Copying, including reproducing, storing,
 * adapting or translating, any or all of this material requires the
 * prior written consent of CMCC.  This material also contains
 * confidential information which may not be disclosed to others
 * without the prior written consent of CMCC.
 */

package com.xgkj.diyiketang.http.engine;

import android.os.Handler;
import android.os.Message;

import com.xgkj.diyiketang.util.PalLog;


/**
 * Class Name : TaskEventHandler Author : Fu Xin Date : 13-9-3 Description :
 */
public class TaskEventHandler extends Handler {

	public static final int TASK_EVENT_COMPLETED = 0;
	public static final int TASK_EVENT_MESSAGE = 1;
	public static final int TASK_EVENT_ERROR = 2;

//	private WeakReference<TaskEventListener> mListener;
	private TaskEventListener mListener;

	public TaskEventHandler(TaskEventListener listener) {
		mListener = listener;
	}

	/**
	 * Notify all listeners that task is completed, and send result and data to
	 * listener.
	 * 
	 * @param result
	 *            task result code
	 * @param data
	 *            the response data when task is completed
	 */
	void handleTaskCompleted(int taskId, int result, Object data) {
		notifyListener(TASK_EVENT_COMPLETED, taskId, result, data);

	}

	/**
	 * Notify all listeners that task is failed, and send error information to
	 * listener.
	 * 
	 * @param errorCode
	 *            the code indicate error type
	 * @param errorMsg
	 *            the string message describe error information
	 */
	void handleTaskError(int taskId, int errorCode, String errorMsg) {
		notifyListener(TASK_EVENT_ERROR, taskId, errorCode, errorMsg);

	}

	/**
	 * Notify all listeners the new task message, message content is stored in
	 * data
	 * 
	 * @param data
	 *            the message content
	 */
	void handleTaskMessage(int taskId, Object data) {
		notifyListener(TASK_EVENT_MESSAGE, taskId, 0, data);
	}

	/**
	 * encapsulate Listener , send message
	 */
	private void notifyListener(int eventType, int taskId, int arg1, Object arg2) {
		Message msg = this.obtainMessage(eventType, taskId, arg1, arg2);
		sendMessage(msg);
	}

	public boolean contains(TaskEventListener listener) {
		if (listener == null) {
			return false;
		}

		if (mListener != null) {
//			TaskEventListener containedListener = mListener.get();
			TaskEventListener containedListener = mListener;
			if (containedListener != null && containedListener == listener) {
				return true;
			}
		}
		return false;
	}

	@Override
	public void handleMessage(Message msg) {

//		TaskEventListener listener = mListener.get();
		TaskEventListener listener = mListener;
		if (listener == null) {
			return;
		}

		switch (msg.what) {
		case TASK_EVENT_COMPLETED:
//		   PalLog.d("TaskEventHandler:", "handleMsg Complete : msg is " + msg);
			listener.handleTaskCompleted(msg.arg1, msg.arg2, msg.obj);
			break;
		case TASK_EVENT_MESSAGE:
			listener.handleTaskMessage(msg.arg1, msg.obj);
			break;
		case TASK_EVENT_ERROR:
//		    PalLog.d("TaskEventHandler:", "handleMsg Error : msg is " + msg);
			listener.handleTaskError(msg.arg1, msg.arg2, (msg.obj == null) ? null : msg.obj.toString());
			break;

		default:
			break;
		}
	}
}
